package ocr;

/**
 * 对图片的文件大小进行放大和缩小
 *
 * @author 啊林
 * @date 2022/4/14
 * @time 23:39
 */

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PictureScale {

    /**
     * @param srcImageFile    源图像文件图像地址
     * @param resultImageFile 缩放后的图像地址
     * @param scale           缩放比例
     * @param flag            缩放选择:true 放大; false 缩小;
     */
    public static void scale(String srcImageFile, String resultImageFile, int scale, boolean flag) {
        try {
            // 读去图片
            BufferedImage src = ImageIO.read(new File(srcImageFile));
            // 图片宽度
            int width = src.getWidth();
            // 图片高度
            int height = src.getHeight();
            if (flag) {
                width = width * scale;
                height = height * scale;
            } else {
                // 缩小
                width = width / scale;
                height = height / scale;
            }
            Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            // 绘制处理后的图片
            g.drawImage(image, 0, 0, null);
            g.dispose();
            // 输出到文件
            ImageIO.write(tag, "JPEG", new File(resultImageFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        scale("C:\\Users\\啊林\\Desktop\\图片检测\\20220522\\15-21-56.bmp", "C:\\Users\\啊林\\Desktop\\图片检测\\20220522\\15-21-56.bmp", 1, true);
    }


}